﻿using Handday.Erp.Core;
using HandDay.Framework.CorrelationId.DependencyInjection;
using HandDay.Logger.Core.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Handday.Erp.JobTask
{
    /// <summary>
    /// program
    /// </summary>
    class Program
    {
        /// <summary>
        /// Main
        /// </summary>
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        /// <summary>
        /// CreateHostBuilder
        /// </summary>
        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, configurationBuilder) =>
                {
                    //分布式配置中心
                    configurationBuilder
                        .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                        .LoadApolloAppSettings();
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddTransient<BaseApiUrl, BaseApiUrl>();

                    //添加分布式缓存
                    services.AddStackExchangeRedisCache(options =>
                    {
                        options.Configuration = hostContext.Configuration["Erp.Transfer.Redis.Host"];
                        options.InstanceName = hostContext.Configuration["erp.transfer_"];
                    });

                    //注册http请求服务
                    services.AddHttpClient();

                    //接入分布式锁，假如会部署多个主机，建议开启，避免任务重复执行
                    services.AddRedLock(hostContext.Configuration);

                    //接入分布式日志
                    services.AddHandDayLogger();
                    services.AddDefaultCorrelationId();

                    // Add the Quartz.NET service
                    services.AddQuartzService();
                });
        }
    }
}
